// my/wx.js
import {
  MyModel
} from '../../models/my'
const myModel = new MyModel()
import {
  AccountModel
} from '../../models/account'
const accountmodel = new AccountModel()
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    my: {},    
    pointMsg: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('11111111')
    // app.editTabBar();
    this.checkOriginPwd()
  },
  loadUserInfo() {
    app.getUserInfo().then(
      user => {
        this.setData({
          user,
        });
      },
      () => {
        // 获取用户信息失败
      }
    );
  },
  loadMyAccount() {
    myModel.loadMyAccount().then(res => {
      console.log(res)
      wx.stationName = '999999'
      this.setData({
        my: res
      })
    })
  },
  checkOriginPwd() {
    accountmodel.checkOriginPwd().then(res => {
        console.log(res)
        if (res && res.code == '00') {
          if (res.pinStatus == 0) {
            app.globalData.ifSetPaw = 0
          } else {
            app.globalData.ifSetPaw = 1
          }
        } else {
          wx.showToast({
            type: 'none',
            content: '接口错误',
            duration: 1000,
          });
        }
        // app.globalData.ifSetPaw= false
      console.log(app.globalData.ifSetPaw)
      })
  },
  toPage(e) {
    const page = e.currentTarget.dataset.url
    console.log(page)
    // if (page =='/pages/invioce/invioce/invioce'){
    //  wx.alert({
    //     title: '很抱歉',
    //     content: '该功能暂未开通，敬请期待~',
    //     buttonText: '我知道了',
    //   });
    //   return
    // }
    // wx.navigateTo({
    //     url: page
    //   })
    //   return
    if (page =='/pages/recharge/recharge'){
      wx.navigateTo({
        url: page
      })
      return
    }
    if (this.data && this.data.my && this.data.wx.phoneNum) {
      wx.navigateTo({
        url: page
      })
    } else {
        wx.getPhoneNumber({
          success: (res) => {
              let encryptedData = res.response;
              app.myoilLogin(encryptedData).then(res => {
                wx.hideLoading();
                console.log('myoilLogin------' + JSON.stringify(res))
                if (res.result == 500) {
                  wx.showToast({
                    type: 'none',
                    content: res.message,
                    duration: 1000,
                  });
                  return
                }
                wx.navigateTo({
                  url: page
                })
              })
          },
          fail: (res) => {
              console.log(res);
              console.log('getPhoneNumber_fail');
              wx.showToast({
                type: 'none',
                content: 'getPhoneNumber~失败',
                duration: 1000,
              });
          },
      });
    }
    // if (page =='/pages/ticket/list/list'){
    //   wx.alert({
    //     title: '很抱歉',
    //     content: '该功能暂未开通，敬请期待~',
    //     buttonText: '我知道了',
    //   });
    // }else{
      // wx.navigateTo({
      //   url: page
      // })
    // }
  },
  goCard() {
    // wx.addCardAuth({
    //   url: 'https://memberprod.alipay.com/account/openform/activecard.htm?app_id=2018052560184366&template_id=20200109000000002172105000300664&__webview_options__=canPullDown%3dNO%26transparentTitle%3dauto&out_string=openCard,CQQP&callback=https://vip.doyenshell.com/AlipayPortal/d/alipay/shopPassed',
    //   success: (res) => {
    //     wx.alert({content: '授权成功'});
    //   },
    //   fail: (res) => {
    //     wx.alert({content: '授权失败'});
    //   },
    // });
    // return
    wx.call('startApp', {
      appId: '20000067',
      param:{
        // url:'https://www.baidu.com',

        url:'https://memberprod.alipay.com/account/openform/activecard.htm?app_id=2018052560184366&template_id=20200109000000002172105000300664&__webview_options__=canPullDown%3dNO%26transparentTitle%3dauto&out_string=openCard,CQQP&callback=https://vip.doyenshell.com/AlipayPortal/d/alipay/shopPassed',
      },
      success: (res) => {
        // wx.alert({
        //   content: JSON.stringify(res)
        // });
      },
      fail: (error) => {
        wx.alert({
          content: '失败了' + JSON.stringify(error)
        });
      }
    });
  },
  // 获取过期积分
  getUserToExpiredPoint() {
    myModel.getUserToExpiredPoint().then(res => {
      console.log(res)
      if (res) {
        if (res.expiredPoints != 0 && res.msg) {
          this.setData({
            'pointMsg': res.msg
          })
        } else {
          this.setData({
            'pointMsg': '',
          })
        }
      } else {
        wx.showToast({
          type: 'none',
          content: '查询积分过期失败',
          duration: 1000,
        });
        this.setData({
          'pointMsg': '',
        })
      }
    })
  },
  showSetPawPop() {

  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.myLogin().then(res => {
      wx.hideLoading();
      this.loadUserInfo()
      this.loadMyAccount()
      this.getUserToExpiredPoint()
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})